using System;
using UnityEngine;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using CustomLitJson;

public class ChannelManager : StuffObject<ChannelManager>
{
    IChannel _selectedChannel;
    string _selecetChannel_cache_name;

    [ShowInInspector]
    public IChannel SelectedChannel
    {
        get
        {
            if(_selecetChannel_cache_name != this.SelectedChannelName)
            {
                _selectedChannel = null;
                _selecetChannel_cache_name = null;
            }
            if(_selectedChannel != null)
            {
                return _selectedChannel;
            }
            var clazzName = this.SelectedChannelName;
            Debug.Log($"[ChannelManager] Try select channel : [{clazzName}]");
            var type = Type.GetType(clazzName);

            if(type == null)
            {
                //throw new Exception("[ChannelManager] type not found for :" + clazzName);
                Debug.LogError("[ChannelManager] type not found for :" + clazzName);
                return null;
            }
            var instance = StuffUtil.GetStuff(type);
            _selectedChannel = instance as IChannel;
            _selecetChannel_cache_name = this.SelectedChannelName;
            Debug.Log($"[ChannelManager] Channel [{clazzName}] selected");
            return _selectedChannel;
        }
    }

    List<string> _channelNameList;

    [ShowInInspector]
    public List<string> ChannelNameList
    {
        get
        {

            if(_channelNameList == null)
            {
                StuffUtil.GetAllSubClassStuff<IChannel>(this.GetType().Assembly);
                var typeList = ReflectionUtil.GetSubClasses<IChannel>(this.GetType().Assembly);
                _channelNameList = new List<string>();
                foreach (var type in typeList)
                {
                    _channelNameList.Add(type.Name);
                }
            }
            return _channelNameList;
        }
    }

    [ShowInInspector]
    public string DefaultChannelName
    {
        get
        {
            var infoName = GameManifestManager.Stuff.Get("channel.default", "");
            if (IsChannelInfoValid(infoName))
            {
                return infoName;
            }
            var first = ChannelNameList[0];
            return first;
        }
    }

    bool IsChannelInfoValid(string name)
    {
        if(string.IsNullOrEmpty(name))
        {
            return false;
        }
        if(ChannelNameList.Contains(name))
        {
            return true;
        }
        return false;
    }

    string _selectedChannelName;

    [ShowInInspector]
    public string SelectedChannelName
    {
        get
        {
            if(_selectedChannelName == null)
            {
                var name = PlayerPrefs.GetString(nameof(SelectedChannelName), "");
                if (!IsChannelInfoValid(name))
                {
                    return DefaultChannelName;
                } 
                _selectedChannelName = name;
            }
            return _selectedChannelName;

        }
        set
        {
            if(!IsChannelInfoValid(value))
            {
                throw new Exception("branch " + value + " not valid");
            }
            PlayerPrefs.SetString(nameof(SelectedChannelName), value);
            _selectedChannelName = value;
        }
    }


    public void DeleteCache()
    {
        PlayerPrefs.DeleteKey(nameof(SelectedChannelName));
    }

    static string FirstCharToUpper(string name)
    {
        var hou = name.Substring(1);
        var qian = name[0];
        var bigChar = Char.ToUpper(qian);
        return bigChar + hou;
    }


}